package com.ruoyi.back.service.impl

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Ok
import com.ruoyi.back.domain.BootPage
import com.ruoyi.back.mapper.BootPageMapper
import com.ruoyi.back.service.BootPageService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.stereotype.Service

@Service
class BootPageServiceImpl : ServiceImpl<BootPageMapper, BootPage>(), BootPageService {


    override fun saveBootPage(bootPage: BootPage): Data<Boolean> =
        bootPage.id?.run {
            Ok("更新成功", updateById(bootPage))
        } ?: kotlin.run {
            Ok("保存成功", save(bootPage))
        }

    override fun bootPagePage(): Page<BootPage> =
        TableSupport.buildPageRequest().let { pageDomain ->
            page(Page(pageDomain.pageNum.toLong(), pageDomain.pageSize.toLong()), KtQueryWrapper(BootPage::class.java)
                .orderByDesc(BootPage::createTime))
                .let { page: Page<BootPage> ->
                    page.apply {
                        records = page.records.takeIf { it.isNotEmpty() }
                            ?.run {
                                map {
                                    it.apply {
                                        staySecondsDesc = "${it.staySeconds}s"
                                    }
                                }
                            }
                    }
                }
        }

    override fun removeBootPage(bootPageIds: String): Data<Boolean> =
        Ok("批量移除成功", removeByIds(bootPageIds.split(",")))
}